import json
import re

import django
from django.shortcuts import render

from django.http import HttpResponse,JsonResponse
from order.models import Order
from publish.models import OrderModels
from util.customId_tools import generate_order_id
from util.token_tools import make_token,check_login

def model_view(request):
    return render(request,'model.html')

def order_view(request,model_code):
    model = OrderModels.objects.get(model_code = model_code)
    return render(request,'order.html',{
        "model_title":model.title
    })

# 订单详情
@check_login
def display_order_view(request,mode_code):
    data = json.loads(request.body)
    order_code = data.get('order_code')
    if not order_code:
        return JsonResponse({'code': 200, 'data': mode_code})
    try:
        order = Order.objects.get(order_code=order_code)
    except:
        return JsonResponse({'code': 10101, 'error': "查询失败!"})
    res_dic = {
        'order_detail': order
    }
    return JsonResponse({'code': 200, 'data': res_dic})

def is_number(string):
    pattern = re.compile(r'^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$')
    return bool(pattern.match(string))

# 上传订单
@check_login
def submit_order_view(request,mode_title):
    data = json.loads(request.body)
    name = data.get('name')

    content = data.get('content')
    phone = data.get('phone')
    price = data.get('price')
    django.utils.timezone.now()
    date_time = data.get('date_time')

    status = 1
    address = data.get('address')
    order_model = OrderModels.objects.get(title = mode_title)
    order_code = generate_order_id(request.login_user.id)

    user = request.login_user
    if not name:
        return JsonResponse({'code': 10101, 'error': '下单人不能为空!'})
    if len(phone) != 11:
        return JsonResponse({'code': 10101, 'error': '请输入正确的手机号!'})
    if not is_number(price):
        return JsonResponse({'code': 10101, 'error': '请输入正确金额格式!'})
    if not content:
        return JsonResponse({'code': 10101, 'error': '内容不能为空!'})
    if not address:
        return JsonResponse({'code': 10101, 'error': '地址不能为空!'})
    if not date_time:
        return JsonResponse({'code': 10101, 'error': '订单时间不能为空!'})
    try:
        Order.objects.create(
            name=name,
            content=content,
            phone=phone,
            price=price,
            date_time=date_time,
            status=status,
            address=address,
            order_model=order_model,
            order_code=order_code,
            user = user
        )
        return JsonResponse({'code': 200})
    except Exception as e:
        print(e)
        return JsonResponse({'code': 10103, 'error': '订单创建失败!'})

#修改订单
@check_login
def update_order_view(request,order_code):
    data = json.loads(request.body)
    name = data.get('name')
    content = data.get('content')
    phone = data.get('phone')
    price = data.get('price')
    django.utils.timezone.now()
    date_time = data.get('date_time')
    print(f"date_time = {date_time}")

    address = data.get('address')
    order_code = generate_order_id(order_code)
    try:
        order = Order.objects.get(order_code=order_code)
        order.name = name
        order.content = content
        order.phone = phone
        order.price = price
        order.date_time = date_time
        order.address = address
        order.save()
        return JsonResponse({'code': 200})
    except Exception as e:
        print(e)
        return JsonResponse({'code': 10103, 'error': '订单修改失败!'})


def order_reqeust(request):
    models = OrderModels.objects.all()
    models_list = []
    for item in models:
        models_dic = {
            'title': item.title,
            'desc': item.desc,
            'model_code': item.model_code,
            'model_img': str(item.model_img),
        }
        models_list.append(models_dic)
    res_dic = {
        'models': models_list
    }
    return JsonResponse({'code': 200, 'data': res_dic})
